<?php
class TermsTaxonomyMeta extends CActiveRecord {
	const KEY_CATEGORY_PIC = 'categoryPic';
	const KEY_TOP_POST = 'topPost';
	const PIC_EXT = '.jpg';
	
	const CATEGORY_PIC_SUFFIX_M = 'M';
	const CATEGORY_PIC_WIDTH_M = 135;
	const CATEGORY_PIC_HEIGHT_M = 90;
	const CATEGORY_PIC_SUFFIX_L = 'L';
	const CATEGORY_PIC_WIDTH_L = 220;
	const CATEGORY_PIC_HEIGHT_L = 145;
	
	public $categoryPic;
	
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}
	
	public function tableName() {
		return strtolower('TermsTaxonomyMeta');
	}

//	public function rules() {
//		
//	}
	
//	public function safeAtrributes() {
//		
//	}
//	
	public function relations() {
		return array(
			'categoryPic' => array(self::BELONGS_TO, 'TermsTaxonomy', 'metaId',
				'on' => 'termTaxonomyId = metaId',
				'together' => true,
				'joinType' => 'INNER JOIN',
				'condition' => 'metaKey = "'.self::KEY_CATEGORY_PIC.'"'.
					' AND taxonomy = "'.TermsTaxonomy::TAXONOMY_CATEGORY.'"'
			),
			'post' => array(self::BELONGS_TO, 'Posts', 'metaValue',
//				'together' => true,
				'joinType' => 'INNER JOIN'
			)
		);
	}
	
	public static function categoryPicUrl($fn, $suffix = 'M') {
		$fn = pathinfo($fn, PATHINFO_FILENAME);
		$path = UPLOADS_CATEGORIES_PICS.'/'.$fn.'_'.$suffix.self::PIC_EXT;
		$uri = Yii::app()->baseUrl.'/'.$path;
		return is_file($path) ? $uri : false;
	}
}
